﻿using MediatR;
using MS.Data.API.Write.Applicant.Command.RoleInfo;
using MS.Data.Domain;
using MS.Data.ErrorCode;
using MS.Data.Infratructrue.Istanbul;

namespace MS.Data.API.Write.Applicant.CommandHandel.RoleInfo
{
    /// <summary>
    /// 修改角色信息命令处理
    /// </summary>
    public class UpdateCommandHandel : IRequestHandler<UpdateCommand, APIResult<int>>
    {
        /// <summary>
        /// 角色
        /// </summary>
        IBaseRepository<Role> roleRep;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="roleRep"></param>
        public UpdateCommandHandel(IBaseRepository<Role> roleRep)
        {
            this.roleRep = roleRep;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> Handle(UpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.code = APIEnum.更新成功;
            result.mes = APIEnum.更新成功.ToString();
            //测试
            var res = roleRep.GetAll().FirstOrDefault(s => s.RoleId == request.RoleId);

            res.RoleName = request.RoleName;
            res.PessionName = request.PessionName;
            res.State = request.State;
            res.CreaTime=DateTime.Now;

            await roleRep.Update(res);
            return await Task.FromResult(result);
        }
    }
}
